#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/2/22 10:16
# @Author  : 王凯
# @File    : std_no_stage_file_gen.py
# @Project : scrapy_spider
import os
from pathlib import Path

from loguru import logger

from apps.tax_policy.tax_policy.std_template.config_spider_entry import ConfigSpiderData
from apps.tax_policy.tax_policy.std_template.gen_scrapy_spider import ScrapyGenerator
from utils.db.mysqldb import MysqlDB


class StdNoStageFileGen:
    db_api = MysqlDB()
    base_path = (Path(__file__).parent.parent.parent / 'spiders' / 'shanxi').as_posix() + '/'

    def __init__(self):
        pass

    def get_datas(self):
        sql = "select * from template_spider_tax_policy where province = '山西省' and stages is null and state = 0"
        return self.db_api.find(sql, to_json=True)

    def run_one(self, data):
        data['stages'] = []
        spider_file_content = ScrapyGenerator(ConfigSpiderData(**data)).generate()

        cache_base_path = self.base_path + 'custom/'
        if not os.path.exists(cache_base_path):
            os.makedirs(cache_base_path, exist_ok=True)
            with open(cache_base_path + '__init__.py', 'w') as f:
                f.write('')

        cache_file_path = cache_base_path + data['name'] + '.py'
        logger.info(f"正在生成文件 {cache_file_path}")
        with open(cache_file_path, "w", encoding="utf-8") as fs:
            fs.write(spider_file_content)

    def run(self):
        datas = self.get_datas()
        for data in datas:
            self.run_one(data)


if __name__ == '__main__':
    test = StdNoStageFileGen()
    test.run()
